<?php

namespace common\model;

use think\model\RelationModel;
use think\module\Module;

class TaskProcessFileModel extends RelationModel
{
    //自动完成
    protected $_auto = [
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],

    ];

    /**
     * 获取器：自定义字段
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getJsonAttr($value, $data)
    {
        // 当前模块的自定义字段配置
        $fieldDict = Module::$moduleDictData['field_index_by_code']['task_process_file']['custom'];

        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        // 把自定义值写入追加字段
        foreach ($fieldDict as $filed => $config) {
            if (array_key_exists($filed, $jsonDictData)) {
                $this->appendCustomField[$filed] = $jsonDictData[$filed];
            } else {
                // 没有值赋空值
                $this->appendCustomField[$filed] = "";
            }
        }

        return [];
    }
}